
local mt = ac.buff['浮空']
mt.displace = true
mt.level = 1
mt.pulse = 0.03

mt.height = 512       --- 飞行高度
mt.speed = 3000       --- 飞行速度

function mt:on_add()
    local unit = self.owner 
    self.now_height = unit:get_height()
    self.max_height = self.now_height + self.height
    self.has_upward = self.now_height <= self.max_height
    self.has_resume = true
    self.has_launch = true 
    unit:notify("单位-打断位移", unit)
    unit:add_restriction "位移"
    unit:add_restriction "幽灵"
    unit:add_restriction "定身"
    self.trigger = unit:on "单位-打断位移" (function (_, _, flag)
        self.has_resume = (flag == true) 
        self:remove()
    end)
    self:notify("on_launch")
end

function mt:on_pulse()
    if self.has_upward then 
        self.now_height = math.min(self.now_height + self.speed * self.pulse, self.max_height)
    else 
        self.now_height = math.max(self.now_height - self.speed * self.pulse, self.max_height)
    end
    self.owner:set_height(self.now_height)
    if self.now_height == self.max_height then 
        self:notify("on_finish")
        self:remove()
    end 
end

function mt:on_remove()
    local unit = self.owner 
    unit:remove_restriction "位移"
    unit:remove_restriction "幽灵"
    unit:remove_restriction "定身"
    if self.trigger then 
        self.trigger:remove()
    end 
    if self.has_resume then 
        unit:set_height( unit:get_default_height() )
    end 
    if self.has_launch then 
        self:notify("on_stop")
    end 
end

function mt:on_cover()
    self.has_resume = false
    return true
end